package com.atguigu.gmall.payment.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.config.AlipayConfig;
import com.atguigu.gmall.payment.service.AlipayService;
import com.atguigu.gmall.payment.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("/api/payment/alipay")
public class AlipayController {
    @Autowired
    private AlipayService alipayService;
    @Autowired
    private PaymentService paymentService;

    //统一收单 下单并支付接口
    @GetMapping("/submit/{orderId}")
    @ResponseBody
    public String submit(@PathVariable Long orderId){
        return alipayService.submit(orderId);
    }

    //同步回调  需要经过网关 http://api.gmall.com/api/payment/alipay/callback/return
    @GetMapping("/callback/return")
    public String callbackReturn(){
        return "redirect:" + AlipayConfig.return_order_url; //挑战到页面微服务的支付成功页面
    }

    //异步回调
    @PostMapping("/callback/notify")
    public String callbackNotify(@RequestParam Map<String, String> paramsMap){
        System.out.println(paramsMap.toString());

        //1:验签 商家存的是支付宝的公钥  自己的私钥  要对发送的支付宝签名进行认证
        try {
            boolean signVerified = AlipaySignature.rsaCheckV1(paramsMap,
                    AlipayConfig.alipay_public_key, //支付宝公钥
                    AlipayConfig.charset,     //utf-8
                    AlipayConfig.sign_type);  //加密方式ras2
            if (signVerified){
                //校验签名成功
                //用户支付的金额是否正确 进行二次校验 验证用户的金额是否正确
                System.out.println("总金额:"+paramsMap.get("total_amount"));

                //订单的后续处理
                //1:交易是否成功
                String tradeStatus = paramsMap.get("trade_status");
                if ("TRADE_SUCCESS".equals(tradeStatus)){
                    //金额是否正确
                    PaymentInfo paymentInfo =  paymentService.selectPayment(paramsMap.get("out_trade_no"));
                    System.out.println(paymentInfo.getTotalAmount().toString());

                    String total_amount = paramsMap.get("total_amount");
                    if (paymentInfo.getTotalAmount().toString().equals(total_amount)){
                        //金额正确
                        //订单完成后 对支付表进行更新
                        paymentService.updatePaymentInfo(paramsMap);
                        return "success";
                    }else {
                        System.out.println("金额不正确");
                        return "failure"; //
                    }
                }else {
                    System.out.println("交易不成功");
                    return "success";
                }
            }else {
                //签名校验不成功
                return "failure";
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "failure";
    }

    //统一收单下单退款接口
    @GetMapping("/refund/{outTradeNo}")
    @ResponseBody
    public Result refund(@PathVariable String outTradeNo){
        alipayService.refund(outTradeNo);
        return Result.ok();
    }
}
